package com.mmlc.meimei.lichengkongjian.Adapter;

import android.content.Context;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.network.responsebean.SearchHintResult;
import com.mmlc.utils.DimonsUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/9/27.
 */
public class SearchHintAdapter<T> extends BaseAdapter {
    private Context mContext;
    private List<T> mList;

    public SearchHintAdapter(Context context) {
        mContext = context;
    }

    public SearchHintAdapter(List<T> list, Context context) {
        mList = list;
        mContext = context;
    }


    public Context getContext() {
        return mContext;
    }

    public void setContext(Context context) {
        mContext = context;
    }

    public List<T> getList() {
        if (mList==null)
            mList=new ArrayList<>();
        return mList;
    }

    public void setList(List<T> list) {
        mList = list;
    }

    @Override
    public int getCount() {
        return mList==null?0:mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            TextView tv = new TextView(getContext());
            AbsListView.LayoutParams layoutParams=new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
                    ,getContext().getResources().getDimensionPixelSize(R.dimen.y120));
            tv.setLayoutParams(layoutParams);
            tv.setGravity(Gravity.CENTER_VERTICAL);
            tv.setBackgroundColor(getContext().getResources().getColor(R.color.text_white));
            tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP
                    ,15);
            tv.setPadding(DimonsUtil.dp2px(getContext()
                    .getResources().getDimensionPixelSize(R.dimen.x32),getContext()),0,0,0);
            tv.setTextColor(getContext().getResources().getColor(R.color.text_title));
            convertView=tv;
        }
        if (getItem(position) instanceof SearchHintResult.DataBean.CategoryBean)
            ((TextView)convertView).setText(((SearchHintResult.DataBean.CategoryBean)getItem(position)).getName());
        if (getItem(position) instanceof SearchHintResult.DataBean.BrandBean)
            ((TextView)convertView).setText(((SearchHintResult.DataBean.BrandBean)getItem(position)).getBrand_name());
        return convertView;
    }
}
